﻿using System;
using System.Collections.Generic;
using System.Text;

namespace MuzičkiFeršterker
{
    /// <summary>
    /// Text pattern searcher class
    /// </summary>
    class PatternSearcher
    {
        private readonly string beginningPattern;
        private readonly string endPattern;

        /// <summary>
        /// Constructor for initializing the pattern searcher
        /// </summary>
        /// <param name="beginningPattern">String with pattern before the returned string</param>
        /// <param name="endPattern">String with pattern after the returned string</param>
        public PatternSearcher(string beginningPattern, string endPattern)
        {
            this.beginningPattern = beginningPattern;
            this.endPattern = endPattern;
        }

        /// <summary>
        /// Returns first pattern match from text
        /// </summary>
        /// <param name="text">Input text, pattern searcher searches in this text</param>
        /// <returns>Returns first pattern match. Returns empty string if no patterns are found</returns>
        public string FirstMatch(string text)
        {
            return FirstMatch(text, false);
        }

        /// <summary>
        /// Returns first pattern match from text with option to trim white spaces
        /// </summary>
        /// <param name="text">Input text, pattern searcher searches in this text</param>
        /// <param name="trimWhiteSpaces">TRUE trims the white spaces on the beginning and on the end of the match</param>
        /// <returns>Returns first pattern match. Returns empty string if no matches are found</returns>
        public string FirstMatch(string text, bool trimWhiteSpaces)
        {        
            int beginningIndex = -1;
            int endIndex = -1;
            int searchIndex = 0;

            if (beginningPattern == "")
            {
                beginningIndex = 0;
            }
            else
            {
                int beginningLength = beginningPattern.Length;
                for (int i = 0; i < text.Length - beginningLength; i++)
                {
                    if (text[i] == beginningPattern[searchIndex])
                    {
                        searchIndex++;
                        if (searchIndex == beginningLength)
                        {
                            beginningIndex = 1 + i;
                            break;
                        }
                    }
                    else
                        searchIndex = 0;
                }
            }
            if (beginningIndex == -1)
                return "";

            if (endPattern == "")
            {
                endIndex = text.Length;
            }
            else
            {
                searchIndex = 0;
                int endLength = endPattern.Length;
                for (int i = beginningIndex; i < text.Length; i++)
                {
                    if (text[i] == endPattern[searchIndex])
                    {
                        searchIndex++;
                        if (searchIndex == endLength)
                        {
                            endIndex = 1 + i;
                            break;
                        }
                    }
                    else
                        searchIndex = 0;
                }
            }

            if (beginningIndex != -1 && endIndex != -1)
            {
                if (trimWhiteSpaces)
                    return text.Substring(beginningIndex, endIndex - beginningIndex - endPattern.Length).Trim(new char[] { (char)9, (char)10, (char)32 });
                else
                    return text.Substring(beginningIndex, endIndex - beginningIndex - endPattern.Length);
            }
            else
                return "";
        }

        /// <summary>
        /// Method that returns all pattern matches in the text
        /// </summary>
        /// <param name="text">Input text, pattern searcher searches in this text</param>
        /// <returns>List of all pattern matches. Returns empty List if no matches are found</returns>
        public List<string> AllMatches(string text)
        {
            return AllMatches(text, false);
        }

        /// <summary>
        /// Method that returns all pattern matches in the text with option to trim white spaces
        /// </summary>
        /// <param name="text">Input text, pattern searcher searches in this text</param>
        /// <param name="trimWhiteSpaces">TRUE trims the white spaces on the beginning and on the end of the match</param>
        /// <returns>Returns first pattern match. Returns empty string if no matches are found</returns>
        public List<string> AllMatches(string text, bool trimWhiteSpaces)
        {
            List<string> vrni = new List<string>();
            int beginningLength = beginningPattern.Length;
            int endLength = endPattern.Length;


            if (beginningPattern == "" || endPattern == "")
            {
                vrni.Add(FirstMatch(text));
            }
            else
            {
                int beginningIndex = -1;
                int endIndex = -1;
                int searchIndex = 0;

                for (int i = searchIndex; i < text.Length; i++)
                {
                    beginningIndex = -1;
                    endIndex = -1;
                    searchIndex = 0;
                    for (; i < text.Length; i++) //iskanje zacetnega stringa
                    {
                        if (text[i] == beginningPattern[searchIndex])
                        {
                            searchIndex++;
                            if (searchIndex == beginningLength)
                            {
                                beginningIndex = 1 + i;
                                break;
                            }
                        }
                        else
                            searchIndex = 0;
                    }
                    i++;    //povečamo, da gremo na naslednje mesto
                    searchIndex = 0;
                    for (; i < text.Length; i++)   //iskanje začetnega stringa
                    {
                        if (text[i] == endPattern[searchIndex])
                        {
                            searchIndex++;
                            if (searchIndex == endLength)
                            {
                                endIndex = 1 + i;
                                break;
                            }
                        }
                        else
                            searchIndex = 0;
                    }

                    if (beginningIndex != -1 && endIndex != -1)
                    {
                        if (trimWhiteSpaces)
                            vrni.Add(text.Substring(beginningIndex, endIndex - beginningIndex - endPattern.Length).Trim(new char[] { (char)9, (char)10, (char)32 }));
                        else
                            vrni.Add(text.Substring(beginningIndex, endIndex - beginningIndex - endPattern.Length));
                    }
                }
            }

            return vrni;
        }
    }
}
